{{- $mainScript := resources.Get "service-worker/index.js" }}
{{- $configScript := resources.Get "service-worker/config.js" | resources.ExecuteAsTemplate "service-worker.config.js" . -}}
{{- $script := slice $configScript $mainScript | resources.Concat "service-worker.js" -}}
{{- if isset $.Site.Params "pwa" -}}
<script>
if ('serviceWorker' in navigator) {
  window.addEventListener('load', () => {
    navigator.serviceWorker.register('{{ $script.Permalink }}').then(function(reg) {
      console.log('Successfully registered service worker', reg);
    }).catch(function(err) {
      console.warn('Error whilst registering service worker', err);
    });
  });
}
</script>
{{- end -}}
